<!DOCTYPE html>
<html lang="en">
<head>
	<meta charset="UTF-8">
	<title>Title</title>
	<script src="lib/react.development.js"></script>
	<script src="lib/react-dom.development.js"></script>
	<script src="lib/babel.min.js"></script>
</head>
<body>
<div id="root"></div>
</body>
<script type="text/babel">
	const root = ReactDOM.createRoot(document.querySelector("#root"));
	class App extends React.Component{
		state = {
			username:"zhangsan",
			age:10
		}
		render(){
			return (
				<div>
					<h3>App</h3>
					<button onClick={()=>this.setState({username:this.state.username+"!"})}>{this.state.username}</button>
					<button onClick={()=>this.setState({age:this.state.age+10})}>{this.state.age}</button>
					<hr/>
					<A username={this.state.username}/>
					<hr/>
					<B age={this.state.age}/>
				</div>
			)
		}
	}
	class A extends React.Component{
		render(){
			console.log("a-render");
			return (
				<h3>A:{this.props.username}</h3>
			)
		}
	}
	class B extends React.Component{
		state = {
			sex:"男"
		}
		shouldComponentUpdate(nextProps,nextState){
			// if(nextProps.age === this.props.age) return false;
			// return true;

			// for(let key in nextProps){
			// 	if(nextProps[key] !== this.props[key]) return true;
			// }
			// for(let key in nextState){
			// 	if(nextState[key] !== this.state[key]) return true;
			// }
			return false;
		}
		render(){
			console.log("b-render")
			return (
				<div>
					<h3>B:{this.props.age}</h3>
					<button onClick={()=>{
						// this.setState({
						// 	sex:this.state.sex+"?"
						// })
						
						this.state.sex += ":"
						
						this.forceUpdate();// 在函数组件中如何实现强制更新？
					}}>{this.state.sex}</button>
				</div>
			
			)
		}
	}
	
	root.render(<App/>)
</script>
</html>